package com.lap.context.starter.entity;

import java.util.Map;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;

@Slf4j
@Builder
@Data
public class UserInfo implements HeaderInfo {

  private final Integer userId;
  private final String userName;
  private final String fullName;
  private final String tenantCode;

  @Getter(AccessLevel.NONE)
  private Map<String, String> map;

  public UserInfo(
      Integer userId,
      String userName,
      String fullName,
      String tenantCode,
      Map<String, String> map) {
    this.userId = userId;
    this.userName = userName;
    this.fullName = fullName;
    this.tenantCode = tenantCode;
    this.map = map;
  }

  @Override
  public Integer getInt(String key) {
    String valueString = getStr(key);
    try {
      return Integer.valueOf(valueString);
    } catch (NumberFormatException e) {
      log.warn("UserInfo number format is error:{}", key);
    }

    log.warn("UserInfo is null");
    return -1;
  }

  @Override
  public String getStr(String key) {
    return MapUtils.isNotEmpty(map) ? map.get(key) : "";
  }
}
